﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using System.Diagnostics;

namespace easteradventure
{
  class DebugHUD : DrawableGameComponent
  {
    const String versionText = "DebugHUD Version 0.0.2";
    Vector2 versionTextPos = new Vector2(0,0);
    Color color = new Color(Color.Wheat,1.0f);
    int interval = 2000,intervalCounter;//using Milliseconds, just picked a time unit and data type

    Game1 game;
    SpriteBatch sb;
    List<Stat> stats;
    SpriteFont debugFont;
    

    public DebugHUD(Game1 game) : base(game)
    {
      this.game = game;
      sb = new SpriteBatch(game.GraphicsDevice);
    }

    public override void Initialize()
    {
      stats = new List<Stat>();
      debugFont = game.Content.Load<SpriteFont>("MenuFont");
    }

    public override void Update(GameTime gameTime)
    {
      intervalCounter += gameTime.ElapsedRealTime.Milliseconds;
      if (intervalCounter > interval)
      {
        intervalCounter -= interval;//Not just setting to 0, so the system may 'catch up' in off-cases
        
        foreach (Stat stat in stats) stat.Update(gameTime);
      }
    }

    public override void Draw(GameTime gameTime)
    {
        sb.Begin();
        sb.DrawString(debugFont, versionText, versionTextPos, color);
        foreach (Stat stat in stats) sb.DrawString(debugFont, stat.DisplayText, stat.Position, color);
        sb.End();
    }

    public void addStat(Stat stat)
    {
      stats.Add(stat);
      if (stats.Count > 1) stat.Position = new Vector2(game.GraphicsDevice.Viewport.Width - stat.BoundingBox.X,stats[stats.Count - 1].Position.Y + stats[stats.Count - 1].BoundingBox.Y);
      else if (stats.Count == 1) stat.Position = new Vector2(game.GraphicsDevice.Viewport.Width - stat.BoundingBox.X,0);
    }
  }

  abstract class Stat
  {
    //Game game;
    protected Vector2 position,boundingBox;
    protected String displayText;

    public Stat()
    {
      //this.game = game;
      position = new Vector2();
    }

    public abstract void Update(GameTime gameTime);

    public String DisplayText
    {
      get
      {
        return displayText;
      }
    }
    public Vector2 BoundingBox
    {
      get
      {
        return boundingBox;
      }
    }
    public Vector2 Position
    {
      get
      {
        return position;
      }
      set
      {
        position = value;
      }
    }
  }

  class ProcessorStat : Stat
  {
    PerformanceCounter cntr;

    public ProcessorStat()
    {
      cntr = new PerformanceCounter("Processor", "% Processor Time", "_Total");
      boundingBox = new Vector2(90,20);//Sets a nice buffer area for the text, didn't find a Dimension object
      displayText = cntr.NextValue() + "%";
    }

    public override void Update(GameTime gameTime)
    {
      displayText = cntr.NextValue() + "%";
    }

  }

  class MemoryStat : Stat
  {
    PerformanceCounter cntr;

    public MemoryStat()
    {
      cntr = new PerformanceCounter("Memory", "Available MBytes");
      boundingBox = new Vector2(70,20);//Sets a nice buffer area for the text, didn't find a Dimension object
      displayText = cntr.NextValue() + "Mb";
    }

    public override void Update(GameTime gameTime)
    {
      displayText = cntr.NextValue() + "Mb";
    }
  }
   
}
